package bookmanage;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Address extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JLabel lblNewLabel;
	private JTextField searchTF;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	MemberInsert mi;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> colName = new Vector<String>();

	public Address(MemberInsert m) {
		this.mi=m;
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				mi.addrFlag=false;
			}
		});
		setTitle("우편번호 검색");
		setBounds(100, 100, 500, 300);
		colName.add("우편번호");
		colName.add("주소");	
		colName.add("번지");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		contentPanel.add(getLblNewLabel());
		contentPanel.add(getSearchTF());
		contentPanel.add(getBtnNewButton());
		contentPanel.add(getScrollPane());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						int row = table.getSelectedRow();
						String zipcode=(String)table.getValueAt(row, 0);
						String addr=(String)table.getValueAt(row, 1);
						mi.zipcode.setText(zipcode);
						mi.addr1.setText(addr);
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		JTableHeader jh = table.getTableHeader();
		jh.setReorderingAllowed(false);
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("검색어");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSearchTF() {
		if (searchTF == null) {
			searchTF = new JTextField();
			searchTF.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					String str = searchTF.getText().trim();
					if(e.getKeyCode()==KeyEvent.VK_ENTER){
						if(str.equals("")){
							JOptionPane.showMessageDialog(null, "검색어를 입력해주세요!");
							return;
						}else{
							selectAddr(str);
						}
					}
				}
			});
			searchTF.setBounds(81, 7, 232, 24);
			searchTF.setColumns(10);
		}
		return searchTF;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String str = searchTF.getText().trim();
					if(str.equals("")){
						JOptionPane.showMessageDialog(null, "검색어를 입력해주세요!");
						return;
					}
					selectAddr(str);
				}
			});
			btnNewButton.setBounds(325, 6, 97, 24);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 36, 460, 183);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			model = new DefaultTableModel(colName, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table = new JTable(model);
		}
		return table;
	}

	public void selectAddr(String str){
		
		model.setRowCount(0);
		
		String sql="select zipcode,\"addr\",bunji from "
				+"(select zipcode, sido || ' ' || gugun || ' ' || dong || ' ' as \"addr\",bunji from zipcode) "
				+"where \"addr\" like '%"+str+"%' ";

		try {
			PreparedStatement psmt = mi.con.prepareStatement(sql);
			//psmt.setString(1, str);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				Vector<String> vec = new Vector<String>();
				vec.add(rs.getString(1));
				vec.add(rs.getString(2));
				vec.add(rs.getString(3));
				model.addRow(vec);
			}
			table.setModel(model);
			rs.close();
			psmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
